package library;

import java.util.ArrayList;
import java.util.List;

public class Library {
	private List<Book> books;

    public Library() {
        books = new ArrayList<>();
    }

    public void addBook(Book book) {
        books.add(book);
        System.out.println("成功添加");
    }

    public void removeBook(int bookId) {
        boolean removed = false;
        for (int i = 0; i < books.size(); i++) {
            Book book = books.get(i);
            if (book.getId() == bookId) {
                books.remove(i);
                removed = true;
                break;
            }
        }
        if (removed) {
            System.out.println("书籍下架成功");
        } else {
            System.out.println("图书馆未存入该书");
        }
    }

    public void updateBookQuantity(int bookId, int quantity) {
        for (Book book : books) {
            if (book.getId() == bookId) {
                book.quantity = quantity;
                System.out.println("图书库存更新成功");
                return;
            }
        }
        System.out.println("该书为入图书馆");
    }

    public void searchBooksByTitle(String title) {
        boolean found = false;
        for (Book book : books) {
            if (book.getTitle().equalsIgnoreCase(title)) {
                System.out.println(bookDetailsToString(book));
                found = true;
            }
        }
        if (!found) {
            System.out.println("该书未入图书馆！");
        }
    }

    public void searchBooksById(int bookId) {
        boolean found = false;
        for (Book book : books) {
            if (book.getId() == bookId) {
                System.out.println(bookDetailsToString(book));
                found = true;
                break;
            }
        }
        if (!found) {
            System.out.println("该书未入图书馆！");
        }
    }

    private String bookDetailsToString(Book book) {
        return "ID: " + book.getId() +
                "\n类别: " + book.getCategory() +
                "\n书名: " + book.getTitle() +
                "\n作者: " + book.getAuthor() +
                "\n出版社: " + book.getPublisher() +
                "\n单价: "

 + book.getPrice() +
                "\n现存量: " + book.getQuantity();
    }

    public ArrayList<Book> getBooks() {
        return (ArrayList<Book>) books;
    }

	
}

